package zunge.lbe.analysis;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import zunge.tools.Tools;

public class Analysis_Classification {
	DataSet dataSet;
	String libFolder;
	
	String folder;
	
	public Analysis_Classification(DataSet dataSet, String libFolder){
		this.dataSet = dataSet;
		this.libFolder = libFolder;
		this.folder = dataSet.folder + "classification/";
	}
	
	public void gen() throws Exception{
		new File(folder).mkdir();
		
		List<String> scripts = new ArrayList<String>();
		
		for(DataSetFold fold:dataSet.folds){
			for(int i = 1;i < 8;++i){
				boolean bu = (i/4==1);
				boolean ba = ((i%4)/2==1);
				boolean bp = (i%2==1);
				String filePrefix = "classification_m"+i+"_v"+fold.v;
				LibLinear ll = new LibLinear(folder, filePrefix);
				int[] flags = new int[]{LibLinear.FLAG_NOMINAL,LibLinear.FLAG_NOMINAL,LibLinear.FLAG_NOMINAL,LibLinear.FLAG_NUMERIC};
				boolean[] valids = new boolean[]{bu, ba, bp, true};
				ll.genData(fold.getFile(fold.fsuf_train), fold.getFile(fold.fsuf_test), flags, valids);
				scripts.add(ll.genScript(libFolder));
			}
		}

		ScriptMgr.splitScript(scripts, 12, folder+"cmd_classification.sh");
	}
	
	public void collectResult() throws Exception{
		BufferedWriter bw = new BufferedWriter(new FileWriter(folder+"result_classification.txt"));
		for(int i = 1;i < 8;++i){
			double[] accuracies = new double[dataSet.V];
			bw.write(i+"\n");
			for(DataSetFold fold:dataSet.folds){
				String filePrefix = "classification_m"+i+"_v"+fold.v;
				LibLinear ll = new LibLinear(folder, filePrefix);
				accuracies[fold.v] = ll.getAccuracy();
				bw.write(accuracies[fold.v]+" ");
			}
			bw.write("\n"+Tools.getAverage(accuracies)+" "+Tools.getVariance(accuracies)+"\n\n");
		}
		bw.close();
	}
}
